//index.js
//获取应用实例
const app = getApp()
const host = require('../../config').host
var self = "false";
var pageData = {
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    host:host,
    phones:"",
    template: {},
    password:"",
    session:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // expert:{name:"侯启军",company:"国际石油经济编辑放无色部",position:"编辑",phone:"300131313",email:"932746553@qq.com",address:"北四环西路68号左岸工社"}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  expertReview: function(){
    wx.navigateTo({
      url: './expertReview'
    })
  },
  expertPersonal: function(){
    wx.navigateTo({
      url: './expertPersonal'
    })
  },
  expertNew: function(){
    wx.navigateTo({
      url: './expertNew'
    })
  },
  adminNew: function(){
    wx.navigateTo({
      url: './adminNew'
    })
  },
  adminLists: function(){
    wx.navigateTo({
      url: './adminLists'
    })
  },
  adminExpert: function(){
    wx.navigateTo({
      url: './adminExpert'
    })
  },
  changeData:function(){
    this.onLoad();//
  },
  toLogin:function(){
    // console.log(this);
    var phones = this.data.phones;
    var password = this.data.password;
    var that = this;
    wx.request({
      url:host+'api/user/login',
      data:{phones:phones,password:password},
      success:function(res){
        console.log(res.data)
        if(res.data.sta == 1){
          that.setData({
            session:1,
          });
          app.globalData.session = 1;
          app.globalData.id = res.data.data.id;
          if(self){
            wx.setStorage({
              key:"phones",
              data:phones
            });
            wx.setStorage({
              key:"password",
              data:password
            });
          }
            wx.setStorage({
              key:"session",
              data:1
            });
            wx.setStorage({
              key:"user_id",
              data:res.data.data.id
            });
          that.expert();
          that.admin();
          wx.showToast({
             title:res.data.msg,
             icon:'success',
             duration:1000
           })
        }else if(res.data.sta == 2){
          that.setData({
            session:2,
          });
          app.globalData.session = 2;
          app.globalData.id = res.data.data.id;
          if(self){
            wx.setStorage({
              key:"phones",
              data:phones
            });
            wx.setStorage({
              key:"password",
              data:password
            });
          }
            wx.setStorage({
              key:"session",
              data:2
            });
            wx.setStorage({
              key:"user_id",
              data:res.data.data.id
            });
          that.expert();
          that.admin();
          wx.showToast({
             title:res.data.msg,
             icon:'success',
             duration:1000
           })
        }else if(res.data.sta == 3){
          that.setData({
            session:3,
          });
          app.globalData.session = 3;
          app.globalData.id = res.data.data.id;
          if(self){
            wx.setStorage({
              key:"phones",
              data:phones
            });
            wx.setStorage({
              key:"password",
              data:password
            });
          }
            wx.setStorage({
              key:"session",
              data:3
            });
            wx.setStorage({
              key:"user_id",
              data:res.data.data.id
            });
          that.expert();
          that.admin();
          wx.showToast({
             title:res.data.msg,
             icon:'success',
             duration:1000
           })
        }else if(res.data.sta == 4){
          wx.showToast({
             title:res.data.msg,
             icon:'none',
             duration:2000
           })
        }
      },
      fail:function(){

      }
    })
  },
  toLogout: function(){
    this.setData({
      session:false
    })
    app.globalData.session = false;
    app.globalData.id = "";
      wx.setStorage({
        key:"session",
        data:false
      });
      wx.setStorage({
        key:"user_id",
        data:""
      });
    wx.showToast({
       title:"退出成功",
       icon:'none',
       duration:2000
     })
    // this.onLoad()
  },
  toAdmin: function(){
    wx.navigateTo({
      url: "./admin"
    })
  },
  userInput:function(e){
    // console.log(e)
    if(e.currentTarget.dataset.mark == "t"){
      this.setData({
        phones:e.detail.value
      })
    }else{
      this.setData({
        password:e.detail.value
      })
    }
    
  },
  onTabItemTap(item) {
    this.onLoad()
  },
  onShow: function(){

  },
  onLoad: function () {
    console.log(this)
    console.log(app.globalData.session)
    // if(app.globalData.session == 'false'){
      this.setData({
        session:app.globalData.session
      })
    // }else{
    //   this.setData({
    //     session:true
    //   })
    // }
    
    var that = this
    wx.getStorage({
      key: 'phones',
      success (res) {
        that.setData({
          phones:res.data
        })
      }
    })
    wx.getStorage({
      key: 'password',
      success (res) {
        that.setData({
          password:res.data
        })
      }
    });
    
    // var id = app.globalData.id;
    // console.log(typeof(app.globalData.id));
    if(typeof(app.globalData.id) != "undefined" && app.globalData.id != ""){
      this.expert();
      this.admin();
    }
  },
  admin:function(){
    var that = this
    wx.request({
      url: host+'api/user/adminMessage',
      data:{expert_id:app.globalData.id},
      success: function(res){
        console.log(res.data)
        that.setData({
          admin: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  expert:function(){
    var that = this
    wx.request({
      url: host+'api/user/expertMessage',
      data:{expert_id:app.globalData.id},
      success: function(res){
        console.log(res.data)
        that.setData({
          experts: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
}

for (var i = 1; i <= 2; ++i) {
  (function (index) {
    pageData[`switch${index}Change`] = function (e) {
      console.log(`switch${index}发生change事件，携带值为`, e.detail.value)
      self = e.detail.value;
      var obj = {}
      obj[`switch${index}Checked`] = e.detail.value
      this.setData(obj)
      obj = {}
      obj[`switch${index}Style`] = e.detail.value ? '' : 'text-decoration: line-through'
      this.setData(obj)
    }
  })(i)
}

Page(pageData)
